let code = require('../../../consts/consts');
let _ = require('lodash');
let userDao = require('../../../dao/userDao');
module.exports = function(app){
    return new Handler(app);
}
let Handler = function(app){
    this.app = app;
    this.redis_db = this.app.get('redis_db');
};
Handler.prototype.creatRoom = async function(msg,session,next){
    let uid = session.uid;
    if(!uid){
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.USER_NOT_EXIST
        });
        return;
    };
    let user = await userDao.queryUserByUid(uid);
    if(!user){
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.USER_NOT_EXIST
        });
        return;
    };
    if(user.gems < 0){
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.GEMS_SHORSTAG
        });
        return;
    };
    if(!msg || !msg.gameNum || msg.isCanBP == undefined || msg.isCanBP == null || !msg.baseScore || !msg.maxSeatNum){
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.PARAM
        });
        return;
    };
    msg.uid = uid;
    let tid = _.random(100000,999999);
    let key = 'ycmj:'+tid;
    let isEixt = await this.redis_db.hgetallAsync(key);
    while(isEixt){
        tid = _.random(100000,999999); 
        key = 'ycmj:'+tid;
        isEixt = await this.redis_db.hgetallAsync(key);
    };
    msg.tid = tid;
    msg.serverId = 0;
    if(!isEixt){
        await this.redis_db.HMSETAsync(key,msg);
    };
    let paramRout = {
        tid:tid,
        uid:uid,
        get:function(str){
            return this[str];
        }
    };
    try {
        console.log('creatRoom=====>',paramRout);
        this.app.rpc.ycmj.ycmjRemote.creatRoom(paramRout,msg,(err,data)=>{
            if(err){
                next(null,err);
            }else
            {
                next(null,data);
            };
        });
    } catch (error) {
        next(null,{
            code:code.FAIL_CODE.CREATR_TABLE_FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:error.stack
        });
    };
};
Handler.prototype.joinRoom = async function(msg,session,next){
    let uid = session.uid;
    if(!uid){
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.USER_NOT_EXIST
        });
        return;
    };
    if(!msg || !msg.tid ){
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.PARAM
        });
        return;
    };
    let key = 'ycmj:'+ msg.tid;
    let res = await this.redis_db.hgetallAsync(key);
    if(!res){
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.ROOM_NOT_EXIST
        });
        return;
    }
    let user = await userDao.queryUserByUid(uid);
    if(!user){
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.USER_NOT_EXIST
        });
        return;
    };
    let frontendId = session.frontendId;
    let player = {
        uid:uid,
        tid:msg.tid,
        name:user.name,
        sex:user.sex,
        img:user.img || '',
        gems:user.gems, 
        sid:frontendId
    };
    msg.player = player;
    let paramRout = {
        tid:msg.tid,
        uid:uid,
        get:function(str){
            return this[str];
        }
    };
    try {
        this.app.rpc.ycmj.ycmjRemote.joinRoom(paramRout,msg,(err,data)=>{
            if(err){
                next(null,err);
            }else
            {
                session.set('tid',msg.tid);
                session.pushAll(()=>{

                });
                next(null,data);
            };
        });
    } catch (error) {
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:error.code.FAIL_CODE.JOIN_TABLE_FAIL
        });
    }
};